﻿using AwesomiumDotNet;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace FormsSample
{
    public partial class WebForm : Form
    {
        WebCore webCore;
        WebView webView;
        Timer timer;

        public WebForm()
        {
            InitializeComponent();

            Resize += WebForm_Resize;
            webViewBitmap.MouseMove += WebForm_MouseMove;
            webViewBitmap.MouseDown += WebForm_MouseDown;
            webViewBitmap.MouseUp += WebForm_MouseUp;
            MouseWheel += WebForm_MouseWheel;
            KeyDown += WebForm_KeyDown;
            KeyPress += WebForm_KeyPress;
            FormClosed += WebForm_FormClosed;
            Activated += WebForm_Activated;
            Deactivate += WebForm_Deactivate;

            WebCoreConfig config = new WebCoreConfig();
            config.SetLogLevel(LogLevel.Verbose);
            config.SetEnablePlugins(true);
            webCore = new WebCore(config);

            webView = webCore.CreateWebView(webViewBitmap.Width, webViewBitmap.Height);
            webView.ChangeCursor += webView_ChangeCursor;
            webView.LoadUrl("http://www.google.com");
            webView.Focus();

            timer = new Timer();
            timer.Interval = 30;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        void WebForm_Activated(object sender, EventArgs e)
        {
            webView.Focus();
        }

        void WebForm_Deactivate(object sender, EventArgs e)
        {
            if (webView.IsDisposed == false)
                webView.Unfocus();
        }

        void WebForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            // Don't allow input after disposal
            webViewBitmap.MouseMove -= WebForm_MouseMove;
            KeyDown -= WebForm_KeyDown;
            KeyPress -= WebForm_KeyPress;

            timer.Dispose();
            webView.Dispose();
            webCore.Dispose();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            webCore.Update();
            if (webView.IsDirty)
                Render();
        }

        void Render()
        {
            RenderBuffer buffer = webView.Render();

            // Create or update bitmap
            if (webViewBitmap.Image == null ||
                (buffer.Width != webViewBitmap.Image.Width || buffer.Height != webViewBitmap.Image.Height))
            {
                webViewBitmap.Image = new Bitmap(buffer.Width, buffer.Height, PixelFormat.Format32bppRgb);
            }

            buffer.ToBitmap((Bitmap)webViewBitmap.Image);
            webViewBitmap.Invalidate();
        }

        void WebForm_Resize(object sender, EventArgs e)
        {
            if (webViewBitmap.Width != 0 && webViewBitmap.Height != 0)
            {
                webView.Resize(webViewBitmap.Width, webViewBitmap.Height);
                webCore.Update();
            }
        }

        void webView_ChangeCursor(object sender, ChangeCursorEventArgs e)
        {
            Cursor = e.Cursor;
        }

        void WebForm_MouseWheel(object sender, MouseEventArgs e)
        {
            webView.InjectMouseWheel(e.Delta);
        }

        void WebForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            WebKeyboardEvent keyboardEvent = new WebKeyboardEvent(WM.Char, (uint)e.KeyChar, 0);
            webView.InjectKeyboardEvent(keyboardEvent);
        }

        void WebForm_KeyDown(object sender, KeyEventArgs e)
        {
            WebKeyboardEvent keyboardEvent = new WebKeyboardEvent(WM.KeyDown, (uint)e.KeyValue, 0);
            webView.InjectKeyboardEvent(keyboardEvent);
        }

        void WebForm_MouseUp(object sender, MouseEventArgs e)
        {
            webView.InjectMouseUp(e.Button);
        }

        void WebForm_MouseDown(object sender, MouseEventArgs e)
        {
            webView.InjectMouseDown(e.Button);
        }

        void WebForm_MouseMove(object sender, MouseEventArgs e)
        {
            webView.InjectMouseMove(e.Location);
        }
    }
}
